package org.jordens.jdbcspy;

import org.jordens.jdbcspy.ui.*;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;

/**
 * A basic Swing UI for displaying JDBCSpy statistics.
 *
 * Makes use of JFreeChart and the Orson extensions.
 *
 * @author Adam Jordens
 */
public class JDBCSpyViewer
{
    private static final JDBCSpyPanel spyPanel = new JDBCSpyPanel();

    public static void main(String[] args)
    {
        SwingUtilities.invokeLater(
            new Runnable()
            {
                public void run()
                {
                    JFrame frame = new JFrame();

                    frame.setLayout(new BorderLayout());

                    frame.getContentPane().add(spyPanel);

                    createJMenuBar(frame);

                    frame.pack();

                    frame.setExtendedState(Frame.MAXIMIZED_BOTH);
                    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

                    frame.setVisible(true);
                }
            }
        );
    }

    /**
     * @return JMenuBar
     */
    private static void createJMenuBar(final JFrame frame)
    {
        JMenuItem connectToItem = new JMenuItem("Connect To...");
        connectToItem.setMnemonic('C');
        connectToItem.addActionListener(
                new ActionListener()
                {
                    public void actionPerformed(ActionEvent event)
                    {
                        String info = JOptionPane.showInputDialog(
                                spyPanel,
                                "Enter host information (hostname:port, default port => 10000)",
                                "localhost:10000");

                        if (info != null)
                        {
                            String[] host = info.split(":");
                            
                            String hostname = host[0];
                            Integer port = 10000;
                            if (host.length > 1)
                            {
                                port = Integer.valueOf(host[1]);
                            }

                            spyPanel.connect(hostname, port);
                        }

                    }
                }
        );

        JMenuItem openLogFileItem = new JMenuItem("Open Log File...");
        openLogFileItem.setMnemonic('O');
        openLogFileItem.addActionListener(
                new ActionListener()
                {
                    public void actionPerformed(ActionEvent event)
                    {
                        JFileChooser fc = new JFileChooser(new File("/tmp/jdbcspy.log"));

                        // Show open dialog; this method does not return until the dialog is closed
                        fc.showOpenDialog(frame);

                        File logFile = fc.getSelectedFile();

                        if (logFile != null)
                        {
                            spyPanel.connect(logFile);
                        }
                    }
                }
        );

        JMenuItem exitItem = new JMenuItem("Exit");
        exitItem.setMnemonic('x');
        exitItem.addActionListener(
                new ActionListener()
                {
                    public void actionPerformed(ActionEvent event)
                    {
                        System.exit(0);
                    }
                }
        );

        JMenu fileMenu = new JMenu("File");
        fileMenu.setMnemonic('F');

        fileMenu.add(connectToItem);
        fileMenu.add(openLogFileItem);
        fileMenu.addSeparator();
        fileMenu.add(exitItem);

        JMenuItem resetItem = new JMenuItem("Reset");
        resetItem.setMnemonic('R');
        resetItem.addActionListener(
                new ActionListener()
                {
                    public void actionPerformed(ActionEvent event)
                    {
                        spyPanel.reset();
                    }
                }
        );

        JMenu viewMenu = new JMenu("View");
        viewMenu.setMnemonic('V');

        viewMenu.add(resetItem);

        JMenuBar menuBar = new JMenuBar();
        menuBar.add(fileMenu);
        menuBar.add(viewMenu);

        frame.setJMenuBar(menuBar);
    }
}
